import store from'@/store';

// let baseurl = "http://api.sibanyun.com:8000";
// let baseurl = '';


function req(obj) {
	let baseurl = '';
	
	// 获取用户选填接口地址 
	if(uni.getStorageSync('curl_port')) {
		baseurl = "http://" + uni.getStorageSync('curl_port').curlname + ":" + uni.getStorageSync('curl_port').curlport;
	}else if(uni.getStorageSync('curl_port_sort')){
		baseurl = "http://" + uni.getStorageSync('curl_port_sort');
	}else {
		baseurl = "http://api.sibanyun.com:8000";
	}
	
	// console.log(baseurl)
	 
	let token = uni.getStorageSync('token'); // 获取token 
	
	uni.setStorageSync('serverUrl',baseurl);
	 
	return new Promise((resolve, reject) => {
		// baseurl 域名
		// const baseurl = "http://api.sibanyun.com:8000"; //公网
		// const baseurl = "http://nosy.vicp.cc:8000" //强
		// const baseurl = "http://192.168.1.2:8000"; //刚
		// const baseurl = "http://192.168.1.172:8000"; //强
		
		let method = obj.method || "GET" ;
		
		let url = baseurl + obj.url || "";
		let data = obj.data || {};
		let header = obj.header || {'Content-Type': obj.contentType || 'application/json'};
		let success = obj.success; // 成功回调
		let fail = obj.fail; //失败回调
		 
		//判断token是否获取，获取就heder携带，
		//APP
		// #ifdef APP-PLUS
		if (token) {header['Access-Token'] = token;}
		// #endif
		
		//h5
		// #ifdef H5
		if (token) {header['access-token'] = token}
		// #endif
		
		// console.log('登录-----------',url) 
		
		uni.getNetworkType({
		    success: function (res) {
		       if(res.networkType !== 'none'){
				   uni.request({
				   	url: url, 
				   	data: data,
				   	method: method,
				   	header: header,
				   	// timeout: 60000,   
				   	success: ((res) => {
				   		// console.log(res)
				   		//登录接口,token携带在header中
				   		
				   		//app
				   		// #ifdef APP-PLUS
				   		if('Access-Token' in res.header){
				   			// if(!token){ 
				   				uni.setStorageSync('token',res.header['Access-Token']);
				   				store.dispatch('getUser');
				   			// }
				   		};
				   		// #endif
				   		
				   		//H5
				   		// #ifdef H5
				   		if('access-token' in res.header){
				   			// if(!token){ 
				   		
				   				console.log(uni.setStorageSync('token',res.header['access-token']));
				   				
				   				store.dispatch('getUser');
				   			// }
				   		};
				   		// #endif
				   		
				   		//token过期，清除token和用户信息,跳转登录页
				   		//401 token过期  403没有访问该功能的权限
				   		if(res.statusCode==401){
				   			uni.showToast({title:res.data.message,icon:'none'});
				   			setTimeout(()=>{
				   				uni.removeStorageSync('token');
				   				uni.removeStorageSync('userInfo');
				   				if(res.data){
				   					if(res.data.code==401){
				   						uni.reLaunch({url:'/components/user/user-dl'})
				   					}else{
				   						resolve(res.data);
				   					}
				   				}else{
				   					uni.reLaunch({url:'/components/user/user-dl'})
				   				}
				   			},1500)
				   		//不是登录接口
				   		}else if(res.statusCode==200 && res.data.code==200){
				   			resolve(res.data);
				   		}else{
				   			if(res.data.code){
				   				// uni.showToast({
				   				// 	title:res.data.message,
				   				// 	icon:'none'
				   				// });
				   				resolve(res.data);
				   			}else{
				   				//登录接口返回 
				   				resolve(res);
				   			}
				   		}
				   	}),
				   	fail: ((err) => {
				   		console.log(err)
				   		uni.showToast({
				   			title: '请求失败!',
				   			duration: 1500,
				   			icon: 'none'
				   		});
				   		reject(err)
				   	})
				   })
			   }else {
				   uni.showToast({ 
						title: '当前网络不可用！',
						icon: 'error'
				   })
				   setTimeout(() => {
				   	uni.navigateTo({
				   		url: "/components/user/user-dl"
				   	})
				   },1500)
			   }
		    }
		});

	})
}

export default req